package bysj.service;


import bysj.dao.GraduateProjectCategoryDao;
import bysj.domain.GraduateProject;
import bysj.domain.GraduateProjectCategory;
import bysj.domain.OpenArea;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

public final class GraduateProjectCategoryService {
	private GraduateProjectCategoryDao graduateProjectCategoryDao = GraduateProjectCategoryDao.getInstance();
	//本类的一个对象引用，保存自身对象
	private static GraduateProjectCategoryService graduateProjectCategoryService =  new GraduateProjectCategoryService();
	//私有的构造方法，防止其它类创建它的对象
	private GraduateProjectCategoryService(){}
	//静态方法，返回本类的惟一对象
	public synchronized static GraduateProjectCategoryService getInstance() {
		return graduateProjectCategoryService;
	}

	public Collection<GraduateProjectCategory> findAll() {
		return graduateProjectCategoryDao.findAll();
	}

	public GraduateProjectCategory find(Integer id) throws SQLException {
		//获得连接以便完成可能的事务操作
		Connection connection = JdbcHelper.getConn();
		GraduateProjectCategory graduateProjectCategory=null;
		//将自动提交设为false,开始事务
		connection.setAutoCommit(false);
		try {
			graduateProjectCategory=graduateProjectCategoryDao.find(id,connection);
			//提交事务
			connection.commit();
		}catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		} finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//关闭连接
			JdbcHelper.close(connection);
		}
		return graduateProjectCategory;
	}

	public boolean update(GraduateProjectCategory graduateProjectCategory) {
		return graduateProjectCategoryDao.update(graduateProjectCategory);
	}

	public boolean add(GraduateProjectCategory graduateProjectCategory) {
		return graduateProjectCategoryDao.add(graduateProjectCategory);
	}

	/*public boolean delete(Integer id) {
		GraduateProjectCategory graduateProjectCategory = this.find(id);
		return this.delete(graduateProjectCategory);
	}*/

	public boolean delete(GraduateProjectCategory graduateProjectCategory) {
		return graduateProjectCategoryDao.delete(graduateProjectCategory);
	}
}
